package com.kingdee.eas.jky.pm.app;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.ui.face.UIRuleUtil;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.DefineProjectFactory;
import com.kingdee.eas.custom.DefineProjectInfo;
import com.kingdee.eas.jky.pm.CostBalanceInfo;
import com.kingdee.eas.jky.pm.WorkFinalAccountsCollection;
import com.kingdee.eas.jky.pm.WorkFinalAccountsFactory;
import com.kingdee.eas.jky.pm.WorkFinalAccountsInfo;

public class CostBalanceControllerBean extends AbstractCostBalanceControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.jky.pm.app.CostBalanceControllerBean");
    protected void _audit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._audit(ctx, pk);
         CostBalanceInfo costInfo = (CostBalanceInfo) this.getValue(ctx, pk);
         if(costInfo.getProjectNumber()!=null){
        	 String projectID=costInfo.getProjectNumber().getId().toString();
        	 DefineProjectInfo projectInfo= DefineProjectFactory.getLocalInstance(ctx).getDefineProjectInfo(new ObjectUuidPK(projectID));
        	 projectInfo.setIsJieS(true);
        	 projectInfo.setSjqrAmount(UIRuleUtil.getBigDecimal(costInfo.getJuesAmount()).add(UIRuleUtil.getBigDecimal(costInfo.getZgyuAmount())));
        	 DefineProjectFactory.getLocalInstance(ctx).update(new ObjectUuidPK(projectID), projectInfo);
        	    EntityViewInfo evInfo = new EntityViewInfo();
		    	FilterInfo filter = new FilterInfo();
		    	filter.getFilterItems().add(new FilterItemInfo("projectNumber.id",projectID));
		    	filter.getFilterItems().add(new FilterItemInfo("status","4"));
		    	evInfo.setFilter(filter);
        	    WorkFinalAccountsCollection  coll =WorkFinalAccountsFactory.getLocalInstance(ctx).getWorkFinalAccountsCollection(evInfo);
        	    if(coll.size()>0){
        	    	WorkFinalAccountsInfo workInfo =coll.get(0);
        	    	workInfo.setIsCostBal(true);
        	    	WorkFinalAccountsFactory.getLocalInstance(ctx).update(new ObjectUuidPK(workInfo.getId()), workInfo);
        	    }
         }
    }
    protected void _unAudit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pk);
         CostBalanceInfo costInfo = (CostBalanceInfo) this.getValue(ctx, pk);
         if(costInfo.getProjectNumber()!=null){
        	 String projectID=costInfo.getProjectNumber().getId().toString();
        	 DefineProjectInfo projectInfo= DefineProjectFactory.getLocalInstance(ctx).getDefineProjectInfo(new ObjectUuidPK(projectID));
        	 projectInfo.setIsJieS(false);
        	 projectInfo.setSjqrAmount(BigDecimal.ZERO);
        	 DefineProjectFactory.getLocalInstance(ctx).update(new ObjectUuidPK(projectID), projectInfo);
        	 EntityViewInfo evInfo = new EntityViewInfo();
		    	FilterInfo filter = new FilterInfo();
		    	filter.getFilterItems().add(new FilterItemInfo("projectNumber.id",projectID));
		    	filter.getFilterItems().add(new FilterItemInfo("status","4"));
		    	evInfo.setFilter(filter);
     	    WorkFinalAccountsCollection  coll =WorkFinalAccountsFactory.getLocalInstance(ctx).getWorkFinalAccountsCollection(evInfo);
     	    if(coll.size()>0){
     	    	WorkFinalAccountsInfo workInfo =coll.get(0);
     	    	workInfo.setIsCostBal(false);
     	    	WorkFinalAccountsFactory.getLocalInstance(ctx).update(new ObjectUuidPK(workInfo.getId()), workInfo);
     	    }
         }
    }
    protected void _unAudit(Context ctx, IObjectPK[] pks)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pks);
         for(int i=0;i<pks.length;i++){
        	 CostBalanceInfo costInfo =  (CostBalanceInfo) this.getValue(ctx, pks[i]);
      		 if(costInfo.getProjectNumber()!=null){
             	 String projectID=costInfo.getProjectNumber().getId().toString();
             	 DefineProjectInfo projectInfo= DefineProjectFactory.getLocalInstance(ctx).getDefineProjectInfo(new ObjectUuidPK(projectID));
//             	 projectInfo.setClosedState(ClosedEnum.nclosed);
             	 projectInfo.setSjqrAmount(BigDecimal.ZERO);
             	 DefineProjectFactory.getLocalInstance(ctx).update(new ObjectUuidPK(projectID), projectInfo);
 		     }
      	}
    }
}